package cn.ichiva.tushare.sdk.tushare.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Data
public class TuRequest implements Serializable {

    /**
     * 接口名
     */
    private String api;
    private String token;
    /**
     * 输入参数
     */
    private Map<String,String> params;
    /**
     * 输出参数
     */
    private String[] fields;
    /**
     * 输出参数描述
     */
    private String[] fieldsName;
    /**
     * 接口描述
     */
    private String describe;

    /**
     * 简化输入
     * @param fieldMap
     */
    public void setFieldMap(Map<String,String> fieldMap) {
        Set<Map.Entry<String, String>> entries = fieldMap.entrySet();
        this.fields = new String[entries.size()];
        this.fieldsName = new String[entries.size()];

        int i = 0;
        for (Map.Entry<String, String> entry : entries) {
            this.fields[i] = entry.getKey();
            this.fieldsName[i] = entry.getValue();
            i++;
        }
    }

    public Object toQuery(){
        HashMap<String, Object> res = new HashMap<>();
        res.put("api_name",getApi());
        res.put("token",getToken());
        res.put("params",getParams());
        res.put("fields", Arrays.stream(getFields()).collect(Collectors.joining(",")));
        return res;
    }
}
